import { Injectable } from '@nestjs/common';
import { CreateTestDto } from './dto/create-test.dto';
import { UpdateTestDto } from './dto/update-test.dto';

import { Repository, Like } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Test } from './entities/test.entity';

@Injectable()
export class TestService {
  constructor(
    @InjectRepository(Test) private readonly testRepository: Repository<Test>,
  ) {
    console.log('testRepository', testRepository);
  }
  create(createTestDto: CreateTestDto) {
    console.log('createTestDto', createTestDto);
    // const data = new Test();
    // data.name = createTestDto.name;
    // data.password = createTestDto.password;
    return this.testRepository.save(createTestDto);
    // return 'This action adds a new testRepository';
  }

  async findAll(query: { keyWord: string; page: number; pageSize: number }) {
    const data = await this.testRepository.find({
      where: {
        name: Like(`%${query.keyWord}%`),
      },
      order: {
        id: 'DESC',
      },
      skip: (query.page - 1) * query.pageSize,
      take: query.pageSize,
    });
    const total = await this.testRepository.count({
      where: {
        name: Like(`%${query.keyWord}%`),
      },
    });
    return {
      data,
      total,
    };
  }
  async findOne(id: number) {
    const firstUser = await this.testRepository.findOneBy({
      id: 1,
    }); // 通过 id 查找
    return firstUser;
  }

  update(id: string, updateTestDto: UpdateTestDto) {
    console.log('updateTestDto', updateTestDto);
    return this.testRepository.update(id, updateTestDto);
  }

  remove(id: string) {
    return this.testRepository.delete(id);
  }
}
